'//DotNetNuke - http://www.dotnetnuke.com
'// Copyright (c) 2002-2010
'// by DotNetNuke Corporation
'//
'// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
'// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
'// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
'// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'//
'// The above copyright notice and this permission notice shall be included in all copies or substantial portions
'// of the Software.
'//
'// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
'// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
'// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
'// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
'// DEALINGS IN THE SOFTWARE.

Imports System
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data.SqlClient
Imports DotNetNuke.Modules.dnnsimplearticle.Components
Imports DotNetNuke.Framework.Providers

Namespace Data

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' SQL Server implementation of the abstract DataProvider class
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class SqlDataProvider

        Inherits DataProvider

#Region "Private Members"

        Private Const ProviderType As String = "data"
        Private Const ModuleQualifier As String = "DNNSimpleArticle_"

        Private _providerConfiguration As ProviderConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType)
        Private _connectionString As String
        Private _providerPath As String
        Private _objectQualifier As String
        Private _databaseOwner As String

#End Region

#Region "Constructors"

        Public Sub New()

            ' Read the configuration specific information for this provider
            Dim objProvider As Provider = CType(_providerConfiguration.Providers(_providerConfiguration.DefaultProvider), Provider)

            ' Read the attributes for this provider

            'Get Connection string from web.config
            _connectionString = Config.GetConnectionString()

            If _connectionString = "" Then
                ' Use connection string specified in provider
                _connectionString = objProvider.Attributes("connectionString")
            End If

            _providerPath = objProvider.Attributes("providerPath")

            _objectQualifier = objProvider.Attributes("objectQualifier")
            If _objectQualifier <> "" And _objectQualifier.EndsWith("_") = False Then
                _objectQualifier += "_"
            End If

            _databaseOwner = objProvider.Attributes("databaseOwner")
            If _databaseOwner <> "" And _databaseOwner.EndsWith(".") = False Then
                _databaseOwner += "."
            End If

        End Sub

#End Region

#Region "Properties"

        Private ReadOnly Property NamePrefix() As String
            Get
                Return DatabaseOwner + ObjectQualifier + ModuleQualifier
            End Get
        End Property

        Public ReadOnly Property ConnectionString() As String
            Get
                Return _connectionString
            End Get
        End Property

        Public ReadOnly Property ProviderPath() As String
            Get
                Return _providerPath
            End Get
        End Property

        Public ReadOnly Property ObjectQualifier() As String
            Get
                Return _objectQualifier
            End Get
        End Property

        Public ReadOnly Property DatabaseOwner() As String
            Get
                Return _databaseOwner
            End Get
        End Property

#End Region

#Region "Private Methods"


#End Region

#Region "Public Methods"

        'Public Overrides Function GetItems(ByVal ModuleId As Integer) As IDataReader
        '    Return CType(SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GetItems"), ModuleId), IDataReader)
        'End Function

        'Public Overrides Function GetItem(ByVal ItemId As Integer) As IDataReader
        '    Return CType(SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GetItem"),ItemId), IDataReader)
        'End Function

        Public Overrides Function GetArticle(ByVal articleId As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "spGetArticle", articleId), IDataReader)
        End Function
        Public Overrides Function GetArticles(ByVal moduleId As Integer, ByVal pageSize As Integer, ByVal pageNumber As Integer) As IDataReader
            Return CType(SqlHelper.ExecuteReader(ConnectionString, NamePrefix + "spGetArticles", moduleId, pageSize, pageNumber), IDataReader)
        End Function


        Public Overrides Function AddArticle(ByVal a As Article) As Integer
            Return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, NamePrefix + "spAddArticle", New SqlParameter("@ModuleId", a.ModuleId), New SqlParameter("@Title", a.Title), New SqlParameter("@Description", a.Description), _
             New SqlParameter("@Body", a.Body), New SqlParameter("@CreatedOnDate", a.CreatedOnDate), New SqlParameter("@CreatedByUserId", a.CreatedByUserId), New SqlParameter("@LastModifiedOnDate", a.LastModifiedOnDate), New SqlParameter("@LastModifiedByUserId", a.LastModifiedByUserId)))
        End Function

        Public Overrides Sub UpdateArticle(ByVal a As Article)
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, NamePrefix + "spUpdateArticle", New SqlParameter("@ArticleId", a.ArticleId), New SqlParameter("@ModuleId", a.ModuleId), New SqlParameter("@Title", a.Title), _
 New SqlParameter("@Description", a.Description), New SqlParameter("@Body", a.Body), New SqlParameter("@LastModifiedOnDate", a.LastModifiedOnDate), New SqlParameter("@LastModifiedByUserId", a.LastModifiedByUserId), New SqlParameter("@ContentItemId", a.ContentItemId))
        End Sub
        Public Overrides Sub DeleteArticle(ByVal articleId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, NamePrefix + "spDeleteArticle", New SqlParameter("@ArticleId", articleId))
        End Sub
        Public Overrides Sub DeleteArticles(ByVal moduleId As Integer)
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, NamePrefix + "spDeleteArticles", New SqlParameter("@ModuleId", moduleId))
        End Sub

#End Region

    End Class

End Namespace